# -*- coding: UTF-8 -*-

try:
    from chardet.universaldetector import UniversalDetector
    IsAuto = True
except  ImportError:
    IsAuto = False

import os
import sys
import argparse
import os.path
import glob
import fnmatch

DEFAULT_ENCODING = 'gb18030'
IsDeleteSourceFile = False
FILE_TYPE = '*.txt'
FILE_SIZE = 2        # minimal 2MB

def convert(filePathName, out_enc=DEFAULT_ENCODING):    
    try:  
        f=open(filePathName,'rb')
        b= b' '

        b+=f.read(4096)
        u=UniversalDetector()
        u.reset()
        u.feed(b)
        u.close()
        f.seek(0)
        b=f.read()
        f.close()
        in_enc=u.result['encoding']
        new_content=b.decode(in_enc, 'ignore')
        f=open(filePathName, 'w', encoding=out_enc)
        f.write(new_content)
        f.close()
        print ("Success: "+filePathName+" converted from "+ in_enc+" to "+out_enc +" !")
    except IOError: 
        print ("Error: "+filePathName+" FAIL to converted from "+ in_enc+" to "+out_enc+" !" )

def appendToFile(src, dest):
    
    try:
        convert(src)
        
        srcFile = open(src, 'r', encoding=DEFAULT_ENCODING)
        
        destFile = open(dest, 'a', encoding=DEFAULT_ENCODING)

        # write the src file base name in to dest file
        destFile.write('\n')
        destFile.write(os.path.basename(src).strip('.txtTXTHTMLhtml'))
        destFile.write('\n')
        
        # write the content
        destFile.write(srcFile.read())
        
        destFile.close()
        srcFile.close() 
        
        if IsDeleteSourceFile:
            os.remove(src)
                
    except:
        print ("Unexpected error:", sys.exc_info()[0])
                   

def explore(dir, destFilePathName, IsLoopSubDIR=True):
    '''Convert files encoding.
    Input:  
        dir         : Current folder
        IsLoopSubDIR:   True -- Include files in sub folder
                        False-- Only include files in current folder
    Output:
        NONE
    '''
    destOriginalName = destFilePathName
    suffixes = 1
    
    if IsLoopSubDIR:
        flist=getSubFileList(dir, FILE_TYPE)
    else:
        flist=getCurrFileList(dir, FILE_TYPE)
    for fname in flist:
        appendToFile(fname, destFilePathName)
        
        # dest file size should below the limit
        if (os.path.getsize(destFilePathName) >= FILE_SIZE*1024*1024):

            print ('The size of ' + os.path.basename(destFilePathName) + ' is over ' + str(FILE_SIZE)+'MB' + '!')
            if '.txt' in destFilePathName:
                destFilePathName = destOriginalName.rstrip('.txt') + str(suffixes) + '.txt'
                suffixes += 1
            print ('    ====>Change dest file name to ' + destFilePathName)
    
def getSubFileList(dir, pat=''):
    '''Get all file list with specified  suffix under current folder(Include sub folder)
    Input:  
        dir     :   Current folder
        suffix  :   default to blank, means select all files.
    Output:
        File list
    '''
    flist=[]
    for root, dirs, files in os.walk(dir):
        for name in files:
            if fnmatch.fnmatch(name, pat):
                flist.append(os.path.join(root,  name))
    return flist

def getCurrFileList(dir, pat=''):
    '''Get all file list with specified suffix under current level folder
    Input:  
        dir     :   Current folder
        suffix  :   default to blank, means select all files.
    Output:
        File list
    '''
    if pat=='':   
        files=glob.glob('*')
    else:
        files = []
        for file in os.listdir('.'):
            if fnmatch.fnmatch(file, pat):
                files += file        
        
    flist=[]    
    for f in files:
        flist.append(os.path.join(dir, f))
    return flist

if __name__ == '__main__':  
    
    argument_parser = argparse.ArgumentParser(description='Combine text files into one file')
    argument_parser.add_argument('-i', dest = 'srcDir', help='the source folder name')
    argument_parser.add_argument('-o', dest = 'destFile', help='the destination file name')
    argument_parser.add_argument('-d', dest = 'erase', action='store_true', help='Erase all source files')
    argument_parser.add_argument('-f', dest = 'fileType', help='Only this file type will be combined. File type/extension name, for e.g. *.txt, *.html. ')
    argument_parser.add_argument('-s', dest = 'fileSize', nargs=1, type=int, help='Dest File size limit(MB), default is 20MB, must >2')
    
    args = argument_parser.parse_args()  
    
    srcDir = args.srcDir
    destFileName = args.destFile
    IsDeleteSourceFile = args.erase
    
    if args.fileType != None:
        FILE_TYPE = args.fileType
        
    if args.fileSize[0] > FILE_SIZE:
        FILE_SIZE = args.fileSize[0];        
            
    if (srcDir == None):
        explore(os.getcwd(), destFileName)
    else:
        explore(srcDir, destFileName)
    
    '''    
    allfiles = os.listdir(srcDirName)
    allfiles.sort()
    

    for eachfile in allfiles:
        print (os.path.join(srcDirName, eachfile))

    
    destFile = open(destFileName, 'a', encoding='gb18030')
    for eachfile in allfiles:
        print (eachfile)
        sourceFilePathName = os.path.join(srcDirName, str(eachfile))
        
        if (os.path.getsize(sourceFilePathName) > 2048):
            #print sourceFilePathName
            srcFile = open(sourceFilePathName, 'r', encoding='gb18030')
            
            # write file name at the beginning
            destFile.write('\n')
            name = str(eachfile.strip('.txt'))            
            destFile.write(name)
            destFile.write('\n')
            
            # write the file content
            destFile.write(srcFile.read())
            srcFile.close()            
        else:
            print ('Ignore ' + sourceFilePathName)
        
        # delete the original file
        os.remove(sourceFilePathName)
        
        # flush the data to file
        destFile.flush()
        
        # check the file size, stop if size over 8MB
        if (os.path.getsize(destFileName) >= 8*1024*1024):
            break
        
    destFile.close()
    '''
